package com.raincat.toolsdemo.vaccin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.raincat.tools.nettools.HttpsConfig;
import com.raincat.tools.nettools.NetCallBack;
import com.raincat.toolsdemo.R;
import com.raincat.toolsdemo.tools.BaseInfo;

import java.util.HashMap;
import java.util.List;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/08/16
 *     desc   : 医院详情
 *     version: 1.0
 * </pre>
 */
public class HospitalDetailActivity extends AppCompatActivity {
    private Context context;
    private TextView tv_hospital, tv_notice;
    private ListView lv_hospital;

    private ListAdapter listAdapter;
    private int hospitalId;
    private HospitalDetailBean hospitalDetailBean;
    private Gson gson;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospitaldetail_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        context = HospitalDetailActivity.this;
        hospitalId = getIntent().getIntExtra("hospitalId", 0);
        gson = new Gson();

        init();
        listener();
        getHospitalDetail();
    }

    private void init() {
        tv_notice = findViewById(R.id.tv_notice);
        tv_hospital = findViewById(R.id.tv_hospital);
        lv_hospital = findViewById(R.id.lv_hospital);
    }

    private void listener() {
        lv_hospital.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(context, HospitalOrderActivity.class);
                intent.putExtra("hospitalId", hospitalId);
                intent.putExtra("productId", hospitalDetailBean.getList().get(position).getId());
                startActivity(intent);
            }
        });

        findViewById(R.id.btn_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getHospitalDetail();
            }
        });
    }

    private void getHospitalDetail() {
        HashMap<String, Object> header = new HashMap<>();
        header.put("Referer", BaseInfo.refererUrl);
        HashMap<String, Object> param = new HashMap<>();
        param.put("lat", BaseInfo.cityInfo.lat);
        param.put("lng", BaseInfo.cityInfo.lng);
        param.put("id", hospitalId);
        param.put("act", "CustomerProduct");
        new HttpsConfig().doGet(BaseInfo.otherUrl, param)
                .headers(header).start(context, new NetCallBack() {
            @Override
            public void result(String result) {
                hospitalDetailBean = gson.fromJson(result, HospitalDetailBean.class);
                if (hospitalDetailBean != null && hospitalDetailBean.getList() != null && hospitalDetailBean.getList().size() != 0) {
                    tv_hospital.setText(hospitalDetailBean.getCname());
                    tv_notice.setText(hospitalDetailBean.getNotice());
                    listAdapter = new ListAdapter(hospitalDetailBean.getList());
                    lv_hospital.setAdapter(listAdapter);
                } else {
                    Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void error(int errorCode, String error) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(500);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                getHospitalDetail();
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private class ListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        private List<HospitalDetailBean.ListBean> mList;

        ListAdapter(List<HospitalDetailBean.ListBean> l) {
            mInflater = LayoutInflater.from(context);
            mList = l;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = mInflater.inflate(R.layout.activity_hospitaldetail_item, parent, false);
                holder.tv_title = convertView.findViewById(R.id.tv_title);
                holder.tv_time = convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            } else
                holder = (Holder) convertView.getTag();

            HospitalDetailBean.ListBean listBean = mList.get(position);
            holder.tv_title.setText(listBean.getText());
            holder.tv_time.setText(listBean.getDate());

            if (listBean.getText().contains("九价"))
                holder.tv_title.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
            else
                holder.tv_title.setTextColor(context.getResources().getColor(android.R.color.black));

            if (listBean.getDate().contains("暂无"))
                holder.tv_time.setTextColor(context.getResources().getColor(android.R.color.black));
            else
                holder.tv_time.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));

            return convertView;
        }

        class Holder {
            TextView tv_title, tv_time;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
